import { defineStore } from "pinia";

import appConfig from "@/config/app.config";

interface UserState {
  token: string;
  tokenExpireTime: number;
  username: string;
  avatar: string;
  roles: string[];
  permissions: string[];
  menus: any[]; // 这里可以根据实际情况定义更具体的类型
  rememberMe: boolean;
  autoLogin: boolean;
  language: string;
}

export const useUserStore = defineStore("user", {
  state: (): UserState => ({
    token: "", // 用户登录token
    tokenExpireTime: -1, // token过期时间
    username: "", // 用户名
    avatar: "", // 用户头像
    roles: [], // 角色列表, 如['admin', 'user']
    permissions: [], // 权限列表, 如['user:create', 'user:update', 'user:delete']
    menus: [], // 菜单列表, 如[{name: '用户管理', path: '/user', icon: 'user', children: [{name: '用户列表', path: '/user/list'}]}]
    rememberMe: false, //记住密码
    autoLogin: false, //自动登录
    language: appConfig.defaultLang(), //语言
  }),
  getters: {
    isLogin: (state) => {
      return (
        state.token &&
        state.tokenExpireTime &&
        new Date().getTime() < new Date(state.tokenExpireTime).getTime()
      );
    },
  },
  actions: {
    setToken(token: string) {
      this.token = token;
    },
    setTokenExpireTime(tokenExpireTime: number) {
      this.tokenExpireTime = tokenExpireTime;
    },
    setUsername(username: string) {
      this.username = username;
    },
    setAvatar(avatar: string) {
      this.avatar = avatar;
    },
    setRoles(roles: string[]) {
      this.roles = roles;
    },
    setPermissions(permissions: string[]) {
      this.permissions = permissions;
    },
    setMenus(menus: any[]) {
      this.menus = menus;
    },
    setRememberMe(rememberMe: boolean) {
      this.rememberMe = rememberMe;
    },
    setAutoLogin(autoLogin: boolean) {
      this.autoLogin = autoLogin;
    },
    setLanguage(language: string) {
      this.language = language;
    },
  },
});
